package com.csi.sai.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import com.csi.sai.bl.login.LoginProcess;
import com.csi.sai.utilities.Constants;
import com.csi.sai.utilities.Utilities;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/web/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private InitialContext envContext;
	private DataSource ds;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
	    try {
			envContext = new InitialContext();
			Context initContext = (Context) envContext.lookup("java:/comp/env");
			ds = (DataSource) initContext.lookup("SAIDB");
		} catch (NamingException e) {
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject jsonReq = JSONObject.fromObject(request.getParameter("jsonReq"));
		JSONObject jsonResponse = new JSONObject();
		HttpSession session = request.getSession();
		
		String process = jsonReq.getString("process");
		
		if(process.equals("salir")) {
			session.invalidate();
			jsonResponse.put("Flag", "true");
		} else {
			String user = jsonReq.getString("usuario");
			String pass = jsonReq.getString("password");
			LoginProcess loginProcess = new LoginProcess(user, pass);
			if(loginProcess.userExists(ds)) {
				createSession(user, pass, loginProcess.getUserName(), session, loginProcess.getType(), loginProcess.getIdUser());
				jsonResponse.put("Flag", "true");
				jsonResponse.put("userName", loginProcess.getUserName());
				jsonResponse.put("type", ""+loginProcess.getType());
			} else {
				jsonResponse.put("Flag", "false");
			}
		}
		
		sendResponse(response, jsonResponse);
	}

	private void createSession(String user, String pass, String userName, HttpSession session, int type, int idUser) {
		session.setAttribute(Constants.SESSION_USER, user);
		session.setAttribute(Constants.SESSION_PASS, pass);
		session.setAttribute(Constants.SESSION_USERTYPE, type);
		session.setAttribute(Constants.SESSION_USERNAME, userName);
		session.setAttribute(Constants.SESSION_IDUSER, idUser);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public void sendResponse(HttpServletResponse response, JSONObject jsonResponse) throws IOException {
		response.setContentType("text/xml; charset=iso-8859-1");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(jsonResponse);
	}
}
